const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderIndex: 0, // 商品
    orderState: '',
    orderList: [],
    type: 'ALL',
    // 评论
    starCheckedImgUrl: '../../images/star1.png',
    starUnCheckedImgUrl: '../../images/star3.png',
    starMap: [
      '非常差',
      '差',
      '一般',
      '好',
      '非常好',
    ],
    starscores: [],
    service_start: 5,
    major_start: 5,
    clothing_start: 5,
    attitude_Start: 5,
  },
  //分类切换
  orderTap: function (e) {
    this.setData({
      type: e.currentTarget.dataset.type,
      page: 0,
      order_list: []
    })
    this.getOrderList();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
      login_status = wx.getStorageSync('login_status');
    if (options && options.type != '') {
      that.setData({
        type: options.type
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this,
      login_status = wx.getStorageSync('login_status');
      that.setData({
        page: 0,
        order_list: []
      })
      if (!login_status || login_status <= 0) {
        wx.showLoading({
          title: '请先进行登陆',
        })
        setTimeout(() => {
          wx.hideLoading({
            success: (res) => {
              wx.navigateTo({
                url: '/pages/grant/grant',
              })
            },
          })
        }, 1500);
      }else{
        that.getOrderType();
        that.getOrderList();
      }

  },
  /**
   * @xk 获取订单类型
   */
  getOrderType: function(){
    var that = this;
    https.post('getBookOrderType',{},true).then(res=>{
      that.setData({
        order_type: res
      })
    })
  },
  /**
   * @xk 获取订单
   */
  getOrderList: function () {
    var that = this,
      type = this.data.type,
      page = this.data.page,
      user_id = wx.getStorageSync('user_id'),
      order_list = this.data.order_list;
    https.post('getBookOrderList', {
      type: type,
      page: page,
      user_id: user_id,
      is_page: 1
    }, false, '正在获取数据').then(res => {
      if(res.list){
        order_list.push.apply(order_list,res.list);
      }
      that.setData({
        order_list: order_list,
        page: res.page
      })
    })
  },
  /**
   * @xk 取消预约
   * @param {} res
   */
  cancel: function(obj){
    var that = this,
        order_sn = obj.currentTarget.dataset.order_sn;
    https.post('cancelBookOrder',{order_sn: order_sn},false,'正在取消').then(res=>{
      if(res){
        that.setData({
          order_list: [],
          page:0,
        })
        that.getOrderList();
      }
    })
  },
  payBookOrder: function(obj){
    const order_sn = obj.currentTarget.dataset.order_sn;
    const order_id = obj.currentTarget.dataset.order_id;
    const orderStatus = this.data.order_list.find(item => item.order_sn === order_sn).status
    console.log('orderStatus', orderStatus)
    var that = this
    https.post('payBookOrder',{order_sn: order_sn,order_id:order_id},false,'正在支付').then(res=>{
      app.globalData.order_sn = res.order_sn;
      if(res.code == 205){
        //执行微信支付
        wx.requestPayment({
          timeStamp: res.pay_info.timeStamp,
          nonceStr: res.pay_info.nonceStr,
          package: res.pay_info.package,
          signType: res.pay_info.signType,
          paySign: res.pay_info.paySign,
          success (res) {
            if(res.errMsg == 'requestPayment:ok'){
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 1500
              })
              if(orderStatus == 0) {
                wx.redirectTo({
                  url: '/pages/appointment_result/appointment_result',
                })
              } else {
                wx.redirectTo({
                  url: '/pages/booking_order/booking_order',
                })
              }
            }else{
              wx.showToast({
                title: '支付取消',
                icon: 'none',
                duration: 2000
              })
              wx.redirectTo({
                url: '/pages/booking_order/booking_order',
              })
            }
          },
          fail (res) {
            wx.showToast({
              title: '支付取消 ',
              icon: 'none',
              duration: 1500
            })
            wx.redirectTo({
              url: '/pages/booking_order/booking_order',
            })
          }
        })
      }else{
        wx.redirectTo({
          url: '/pages/appointment_result/appointment_result',
        })
      }
    })
  },

  /**
   * 分享到朋友圈
   */
  shareToTimeline: function () {
    wx.setClipboardData({
      data: "这周末去收房，幸亏提前找了验房公司！魔尺验房太棒了，找出太多自己看不出的问题，咱不得不说人家验房的就是专业！不光技术好，各方面政策也比咱明白。包括后期应该怎么处理都说到了。谁得收房一定记得找他们，魔尺验房，绝对值得信赖",
      success: function(res) {
        wx.showToast({
          title: "文本已复制，长按保存图片，到朋友圈粘贴文字并选择图片吧",
          duration: 3000
        })
      }
    })
    wx.previewImage({
      current: 'https://wx.amagich.com/ad.jpg', // 当前显示图片的http链接
      urls: ['https://wx.amagich.com/ad.jpg'] // 需要预览的图片http链接列表
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      type: 'ALL',
      page: 0,
      order_list: []
    })
    this.getOrderList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getOrderList();
  },

  /**
   * 拨打验房师电话
   */
  makeCall:function(e){
    let mobile = e.currentTarget.dataset.mobile
    wx.makePhoneCall({
      phoneNumber: mobile //仅为示例，并非真实的电话号码
    })
  },

})
